package com.lance.answer.question_valid;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lance.answer.dto.Section;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Slf4j
public class QuestionAnalyze  {

    public static void main(String[] args) throws IOException {
        String subjectName = "毛泽东思想和中国特色社会主义理论体系概论";
        analyze(subjectName);
    }

    public static void analyze(String subjectName) throws IOException {
        Map<String,String> map = new HashMap<>();

        InputStream inputStream3 = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-quiz.section");
        List<String> quizs = IOUtils.readLines(inputStream3);

        InputStream inputStream4 = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-video.section");
        List<String> videos = IOUtils.readLines(inputStream4);

       for (String quiz:quizs) {
           String url = quiz.split(",")[0];
           String id = url.substring(url.lastIndexOf("id=") + 3);
           map.put(id,url);
       }

        for (String video:videos) {
            String id = video.substring(video.lastIndexOf("mid=") + 4);
            map.put(id,video);
        }
        File file = new File("src/main/resources/" + subjectName + "/valid_sections_error/");
        Collection<File> files = FileUtils.listFiles(file,null,false);
        System.out.println("待分析用户数量：" + files.size());
//        for (File file1:files) {
//            String fileName = file1.getName();
//            String str1 = FileUtils.readFileToString(file1,"utf-8");
//            List<Section> sections = JSONObject.parseArray(str1,Section.class);
//            String userName = fileName.replace(".json","");
//            System.out.println(userName + "," + "节点数：" + sections.size());
//
//            List<Section> notFinished = new ArrayList<>();
//            int i = 0;
//            for (Section section:sections) {
//                String url = section.getUrl().trim();
//                String id = url.substring(url.lastIndexOf("id=") + 3);
//                if (!map.containsKey(id)) {
//                    log.info("用户：{} 节点：{} 不需要刷题",userName,url);
//                    continue;
//                }
//                if (!section.getFinished()) {
//                    notFinished.add(section);
//                } else {
//                    log.info("用户：{}, 节点：{} 刷题完毕",userName,map.get(id));
//                    i++;
//                }
//            }
//
//            if (!CollectionUtils.isEmpty(notFinished)) {
//                File file3 = new File("src/main/resources/" + subjectName + "/section_not_finished/" + userName + ".json");
//                FileUtils.write(file3,JSONObject.toJSONString(notFinished, SerializerFeature.PrettyFormat),"utf-8");
//            }
//            log.info("用户 ：{} ，完成度: {} / {}",userName,i,map.size());
//        }
    }


}
